package com.easyOrder.callback;

import com.aliyun.openservices.ons.api.Message;
import com.easyOrder.core.mq.Callback;
import com.easyOrder.core.mq.TopicTag;
import com.easyOrder.model.Requisition;
import com.easyOrder.param.requisition.RequisitionOrderParam;
import com.easyOrder.service.enums.RequisitionDetailState;
import com.easyOrder.service.enums.RequisitionState;
import com.easyOrder.service.message.MessageService;
import com.easyOrder.service.mq.MQRecordService;
import com.easyOrder.service.requisition.RequisitionService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 下单回调
 * Created by liutao on 2017/7/21.
 */
public class RequisitionOrderCallback implements Callback {

    @Autowired
    private RequisitionService requisitionService;

    @Autowired
    private MQRecordService mqRecordService;

    @Autowired
    private MessageService messageService;

    @Override
    public void execute(Message message) {

        String messageBody = new String(message.getBody());
        RequisitionOrderParam param = new Gson().fromJson(messageBody, RequisitionOrderParam.class);

        Requisition requisition = requisitionService.findById(param.getRequisitionId());

        mqRecordService.add(requisition.getBranchId(),
                new String(message.getBody()),
                message.getMsgID(),
                requisition.getTenantId(),
                getTag().getValue(),
                param.getSession() == null ? null : param.getSession().getUserId(),
                param.getSession() == null ? null : param.getSession().getEmployeeId());

        requisitionService.updateRequisitionState(param.getRequisitionId(), RequisitionState.pay.getValue(), RequisitionDetailState.pay.getValue(),null);
        messageService.pushRequisitionOrderMessage(requisition);
    }

    @Override
    public TopicTag getTag() {
        return TopicTag.requisitionOrder;
    }
}
